//
//  HyqTabBarController.swift
//  约麻
//
//  Created by rimi on 2017/7/12.
//  Copyright © 2017年 Sweet. All rights reserved.
//

import UIKit
import AVOSCloud
import Hyphenate
import MBProgressHUD

class HyqTabBarController: UITabBarController {
    var tabbarImg : UIImageView?
    var tabbarImgeSelceted : UIImageView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildViewController()
        self.delegate = self
    }
    
    func setupChildViewController() {
        self.setUpOneChildViewController(viewController: UIStoryboard(name: "HyqTeaController", bundle: nil).instantiateViewController(withIdentifier: "HyqTeaController"), image: UIImage.init(named: "茶室")!, selectedImage: UIImage.init(named: "茶室")!, title: "茶吧")
        self.setUpOneChildViewController(viewController: HyqAppointmentController(), image: UIImage.init(named: "约牌")!, selectedImage: UIImage.init(named: "约牌")!, title: "约麻")
        self.setUpOneChildViewController(viewController: HyqCircleController(), image: UIImage.init(named: "活动")!, selectedImage: UIImage.init(named:
                 "活动")!, title: "活动")
        let vv = UIStoryboard(name: "Me", bundle: nil).instantiateViewController(withIdentifier: "HyqMeController") as! HyqMeController
        self.setUpOneChildViewController(viewController: vv, image: UIImage.init(named: "约见")!, selectedImage: UIImage.init(named:
            "约见")!, title: "我的")
        
    }
    func setUpOneChildViewController(viewController: UIViewController, image: UIImage, selectedImage: UIImage, title: NSString) {
        
        // 让图片显示图片原始颜色  “UIImage” 后+ “.imageWithRenderingMode(.AlwaysOriginal)”
        viewController.tabBarItem = UITabBarItem.init(title: title as String, image: image, selectedImage: selectedImage.withRenderingMode(.alwaysOriginal))
        self.addChildViewController(viewController)
    }
    
}

extension HyqTabBarController:UITabBarControllerDelegate{
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        /*UINavigationController *navC = (UINavigationController *)viewController;
         ZWHMineViewController *mineVC = navC.viewControllers[0];
         if ([mineVC isKindOfClass:[ZWHMessageViewController class]]) {
         if ([[NSUserDefaults standardUserDefaults]boolForKey:@"islogin"]) {
         return YES;
         }else{
         //[self performSegueWithIdentifier:@"loginAction" sender:nil];
         UINavigationController *loginVc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"loginNavc"];
         ZWHLoginViewController *login = loginVc.viewControllers[0];
         login.delegate = self;
         [self presentViewController:loginVc animated:YES completion:nil];
         return NO;
         }
         }else
         return YES;*/
        if viewController.isKind(of: HyqAppointmentController.self) == true || viewController.isKind(of: HyqMeController.self) == true {
            if AVUser.current() != nil{
                return true
            }else{
                let vc = UIStoryboard(name: "ZWHLogin", bundle: nil).instantiateViewController(withIdentifier: "ZWHLoginViewController") as! ZWHLoginViewController
                vc.delegate = self
                if viewController.isKind(of: HyqAppointmentController.self) == true {
                    vc.page = 1
                }else{
                    vc.page = 3
                }
                present(UINavigationController(rootViewController: vc), animated: true, completion: nil)
                return false
            }
        }else{
            return true
        }
        
    }
}

extension HyqTabBarController:ZWHLoginViewControllerDelegate{
    func showselectIndex(page: Int) {
        selectedIndex = page
    }
}
